package cn.jiedh.app.meiweidk.Utils.tool;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

/**
 * Toast统一管理类
 *
 * @author way
 */
public class T {
	/**
	 * 短时间显示Toast
	 *
	 * @param mContext
	 * @param message
	 */
	private static Toast toast = null;
	public static int LENGTH_LONG = Toast.LENGTH_LONG;
	public static int LENGTH_SHORT = Toast.LENGTH_SHORT;
	private static Context mContext;

	public static void init(Context context) {
		mContext = context;
	}

	/**
	 * 普通文本消息提示
	 *
	 * @param text
	 * @param duration
	 */
	public static void TextToast(final CharSequence text,
	                             final int duration) {
		if (TextUtils.isEmpty(text.toString().trim()))
			return;
		Looper myLooper = Looper.myLooper();
		Looper mainLooper = Looper.getMainLooper();
		if (myLooper != mainLooper) {
			new Handler(Looper.getMainLooper()).post(new Runnable() {
				@Override
				public void run() {
					// 创建一个Toast提示消息
					toast = Toast.makeText(mContext, text, duration);
					// 设置Toast提示消息在屏幕上的位置
					toast.setGravity(Gravity.CENTER, 0, 0);
					// 显示消息
					toast.show();
				}
			});
		} else {
			// 创建一个Toast提示消息
			toast = Toast.makeText(mContext, text, duration);
			// 设置Toast提示消息在屏幕上的位置
			toast.setGravity(Gravity.CENTER, 0, 0);
			// 显示消息
			toast.show();
		}

	}

	/**
	 * 短暂显示Toast提示(来自String)
	 **/
	public static void showShortToast(String message) {
		// Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
//		if (toast == null) {
//			toast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT);
//		} else {
//			toast.setText(text);
//			toast.setDuration(Toast.LENGTH_SHORT);
//		}
//		toast.show();

		if (null == toast) {
			toast = Toast.makeText(mContext, message, Toast.LENGTH_SHORT);
			// toast.setGravity(Gravity.CENTER, 0, 0);
		} else {
			toast.setText(message);
		}
		toast.show();
	}

	/**
	 * 短暂显示Toast提示(来自String)
	 **/
	public static void showShortCenterToast(String text) {
		// Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
		if (toast == null) {
			toast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT);
		} else {
			toast.setText(text);
			toast.setDuration(Toast.LENGTH_SHORT);
		}
		toast.setGravity(Gravity.CENTER,0,0);
		toast.show();
	}

	public static void TextToast(final CharSequence text,
	                             final int duration, final boolean isShow) {
		if (TextUtils.isEmpty(text.toString().trim()))
			return;
		Looper myLooper = Looper.myLooper();
		Looper mainLooper = Looper.getMainLooper();
		if (myLooper != mainLooper) {
			new Handler(Looper.getMainLooper()).post(new Runnable() {
				@Override
				public void run() {
					// 创建一个Toast提示消息
					toast = Toast.makeText(mContext, text, duration);
					// 设置Toast提示消息在屏幕上的位置
					toast.setGravity(Gravity.CENTER, 0, 0);
					// 显示消息
					if (isShow)
						toast.show();
				}
			});
		} else {
			// 创建一个Toast提示消息
			toast = Toast.makeText(mContext, text, duration);
			// 设置Toast提示消息在屏幕上的位置
			toast.setGravity(Gravity.CENTER, 0, 0);
			// 显示消息
			if (isShow)
				toast.show();
		}

	}

	/**
	 * 带图片消息提示
	 *
	 * @param ImageResourceId
	 * @param text
	 * @param duration
	 */
	public static void ImageToast(int ImageResourceId,
	                              CharSequence text, int duration) {
		// 创建一个Toast提示消息
		toast = Toast.makeText(mContext, text, Toast.LENGTH_LONG);
		// 设置Toast提示消息在屏幕上的位置
		toast.setGravity(Gravity.CENTER, 0, 0);
		// 获取Toast提示消息里原有的View
		View toastView = toast.getView();
		// 创建一个ImageView
		ImageView img = new ImageView(mContext);
		img.setImageResource(ImageResourceId);
		// 创建一个LineLayout容器
		LinearLayout ll = new LinearLayout(mContext);
		// 向LinearLayout中添加ImageView和Toast原有的View
		ll.addView(img);
		ll.addView(toastView);
		// 将LineLayout容器设置为toast的View
		toast.setView(ll);
		// 显示消息
		toast.show();
	}

	public static void toastCancel() {
		if (null == toast)
			toast.cancel();
	}

	public static void showShort( CharSequence message) {
		if (null == toast) {
			toast = Toast.makeText(mContext, message, Toast.LENGTH_SHORT);
			// toast.setGravity(Gravity.CENTER, 0, 0);
		} else {
			toast.setText(message);
		}
		toast.show();
	}

	/**
	 * 短时间显示Toast
	 *
	 * @param message
	 */
	public static void showShort( int message) {
		if (null == toast) {
			toast = Toast.makeText(mContext, message, Toast.LENGTH_SHORT);
			// toast.setGravity(Gravity.CENTER, 0, 0);
		} else {
			toast.setText(message);
		}
		toast.show();
	}

	/**
	 * 长时间显示Toast
	 *
	 * @param message
	 */
	public static void showLong( CharSequence message) {
		if (null == toast) {
			toast = Toast.makeText(mContext, message, Toast.LENGTH_LONG);
			// toast.setGravity(Gravity.CENTER, 0, 0);
		} else {
			toast.setText(message);
		}
		toast.show();
	}

	/**
	 * 长时间显示Toast
	 *
	 * @param message
	 */
	public static void showLong( int message) {
		if (null == toast) {
			toast = Toast.makeText(mContext, message, Toast.LENGTH_LONG);
			// toast.setGravity(Gravity.CENTER, 0, 0);
		} else {
			toast.setText(message);
		}
		toast.show();
	}

	/**
	 * 自定义显示Toast时间
	 *  @param message
	 *
     */
	public static void show(CharSequence message) {
		if (null == toast) {
			toast = Toast.makeText(mContext, message, Toast.LENGTH_LONG);
			// toast.setGravity(Gravity.CENTER, 0, 0);
		} else {
			toast.setText(message);
		}
		toast.show();
	}

	/**
	 * 自定义显示Toast时间
	 *
	 * @param message
	 * @param duration
	 */
	public static void show( int message, int duration) {
		if (null == toast) {
			toast = Toast.makeText(mContext, message, duration);
			// toast.setGravity(Gravity.CENTER, 0, 0);
		} else {
			toast.setText(message);
		}
		toast.show();
	}

	/**
	 * Hide the toast, if any.
	 */
	public static void hideToast() {
		if (null != toast) {
			toast.cancel();
		}
	}
}
